<?php
class DataRowManager{

	public $tablename;
	public $id;
	
	public function __construct($table, $rowid = null){
		mysql_ping() or ex_throw(new Exception("DataRowManager::_construct($table, $rowid) Mysql szerver nem működik."));
		$this->tablename=$table;
		$this->id=$rowid;
	}
	
	public function setId($id){
		$this->id=$id;
	}

	public function get_id($col, $value){
		return $this->getId($col, $value);
	}
	
	public function getId($col, $value = null){ // ha nincs value akkor a $col-t a WHERE utáni kifejezésnek vesszük
		if(is_null($value)){ // sima where-es mód
			$s=@mysql_fetch_assoc(mysql_query("SELECT id FROM ".$this->tablename." WHERE ".$col));
			if(!$this->is_row($s['id'])){
				throw new Exception("DataRowManager::getID($col, $value) Nem létezik olyan sor a ".$this->tablename." táblában ahhol teljesül '$col'");
			}
		}
		else{ // mező, érték mód
			$value = str_replace("'", "\\'", $value); // vmi kis védelem az SQL injection ellen
			$s=@mysql_fetch_assoc(mysql_query("SELECT id FROM ".$this->tablename." WHERE ".$col."='".$value."'"));
			if(!$this->is_row($s['id'])){
				throw new Exception("DataRowManager::getID($col, $value) Nem létezik olyan sor a ".$this->tablename." táblában ahhol $col=$value");
			}
		}
		
		return $s['id'];
	}
	
	public function is_row($id){ // alias
		return $this->isRow($id);
	}
	
	public function isRow($id){
		$id = (int) $id;
		if (@mysql_fetch_assoc(mysql_query("SELECT * FROM ".$this->tablename." WHERE id=".$id)) ) {
			return true;
		}
		else {
			return false;
		} 
	}

	public function own(){
		try{
			if($this->get("userid")==$_SESSION['userid']){
				return true;
			}
			return false;
		}
		catch(Exception $e){
			return false;
		}
	}
	
	public function get($col){   ///alias
		return $this->__get($col);
	}
	
	public function __get($col){
	
		if(is_null($this->id)){
			throw new InvalidArgumentException("DataRowManager::get($col)rowid=null !");
		}
		if(!$this->is_row($this->id)){
			throw new Exception("DataRowManager::get($col) a (id=".$this->id.") sor nem létezik a ".$this->tablename." táblában.");
		}
		$q = mysql_query("SELECT $col FROM ".$this->tablename." WHERE id=".$this->id);
		$s = mysql_fetch_assoc($q) or ex_throw(new Exception("NULL at query 'SELECT $col FROM ".$this->tablename." WHERE id=".$this->id."'"));
		if($col=="*"){
			return $s;
		}
		else{
			return $s[$col];
		}
	}
	
	public function set($col, $val){   ///alias
		return $this->__set($col, $val);
	}
	
	
	public function __set($col, $val){
		$value = str_replace("'", "\\'", $value); // vmi kis védelem az SQL injection ellen
	
		if(is_null($this->id)){
			throw new InvalidArgumentException("DataManager::set() rowid=null!");
		}
		if(!$this->is_row($this->id)){
			throw new Exception("DataRowManager::set($col, $val) a (id=".$this->id.") sor nem létezik.");
		}
		return mysql_query("UPDATE ".$this->tablename." SET $col='".$val."' WHERE id=".$this->id);
	}
	
     public function destroy(){
		return mysql_query("DELETE FROM ".$this->tablename." WHERE id=".$this->id);
	}
	
	
	
	public static function create($table, $a){
		foreach($a as $k => $v){ //it's so simple   foreach $array as $key=>$value ---- szétbontjuk 2 tömbbe a neveket és értékeket
			$sql1[] = $k;
			$sql2[] = $v;
		}
		$sql = "INSERT INTO $table (".implode(", ",$sql1).") VALUES ('".implode("', '",$sql2)."')";
		mysql_query($sql) or ex_throw(new Exception("DataRowManager::create($table, $a) Mysql hiba a '$sql' lekérdezésben\n".mysql_error()));
		return new DataRowManager($table, mysql_insert_id());
	}
	
}
?>
